package com.wb.adutils.ads.pangle

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.*
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants
import com.wb.adutils.tools.LogC


class PangleSplashUtils(val adEntity: AdEntity?): AdBaseLoader(adEntity) {

    private var mTTSplashAd: TTSplashAd? = null

    override fun excLoadAd(activity: Activity) {
        try {
            isAdLoadSuccess = false
            // 与初始化广告
            val adSlot = AdSlot.Builder()
                .setCodeId(adEntity?.adUnit)
                .setImageAcceptedSize(width, height)
                .setAdLoadType(TTAdLoadType.LOAD)//推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build()
            val mTTAdNative = TTAdSdk.getAdManager().createAdNative(activity)
            mTTAdNative.loadSplashAd(adSlot, object : TTAdNative.SplashAdListener {
                override fun onError(code: Int, message: String?) {
                    LogC.e("pangle -> 开屏 -> onSplashAdLoadFail...$message")
                    excAdLoadFail(code)
                }

                override fun onTimeout() {
                    LogC.e("pangle -> 开屏 -> onAdLoadTimeout..." )
                    excAdLoadFail(AdConstants.AD_ERROR_CODE_TIMEOUT)
                }

                override fun onSplashAdLoad(ad: TTSplashAd?) {
                    if (timeoutDisposable?.isDisposed == true) { // 已超时
                        LogC.e("pangle -> 开屏 -> onSplashAdLoadSuccess...已超时")
                        return
                    }
                    LogC.e("pangle -> 开屏 -> onSplashAdLoadSuccess...")
                    mTTSplashAd = ad
                    isAdLoadSuccess = true
                    timeoutDisposable?.dispose()
                    listener?.onAdLoaded(null)
                }
            }, timeout)
            LogC.e("pangle -> 开屏 -> 准备加载广告...")
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }


    override fun showAd(adRoot: ViewGroup?) {
        try {
            LogC.e("pangle -> 开屏 -> showAd...")
            if (adRoot == null) {
                release()
                return
            }
            if (isAdLoadSuccess && mTTSplashAd != null) {
                mTTSplashAd?.setSplashInteractionListener(object : TTSplashAd.AdInteractionListener {
                    override fun onAdClicked(p0: View?, p1: Int) {
                        LogC.e("pangle -> 开屏 -> showAd...onAdClicked...")
                        listener?.onAdClicked()
                    }

                    override fun onAdShow(p0: View?, p1: Int) {
                        LogC.e("pangle -> 开屏 -> showAd...onAdShow...")
                        listener?.onAdDisplay()
                    }

                    override fun onAdSkip() {
                        LogC.e("pangle -> 开屏 -> showAd...onAdSkip...")
                        listener?.onAdComplete()
                        release()
                    }

                    override fun onAdTimeOver() {
                        LogC.e("pangle -> 开屏 -> showAd...onAdDismiss...")
                        listener?.onAdComplete()
                        release()
                    }
                })

                adRoot.removeAllViews()
                adRoot.visibility = View.VISIBLE
                adRoot.addView(mTTSplashAd?.splashView)
            } else {
                LogC.e("pangle -> 开屏 -> showAd...广告无效...")
                listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_NOT_LOAD_SUCCESS)
                release()
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun release() {
        super.release()
        mTTSplashAd = null
    }
}